package com.codejiwei.core.graphx

import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.graphx.{Edge, Graph, VertexId}
import org.apache.spark.rdd.RDD

object GraphX_Information {
  def main(args: Array[String]): Unit = {
    val conf = new SparkConf().setMaster("local[1]").setAppName("GraphDemo")
    val sc = new SparkContext(conf)
    //创建点RDD
    val users: RDD[(VertexId, (String, String))] =
      sc.parallelize(Seq((3L, ("rxin", "student")), (7L, ("jgonzal", "postdoc")),
        (5L, ("franklin", "prof")), (2L, ("istoica", "prof"))))

    //创建边RDD
    val relationships: RDD[Edge[String]] =
      sc.parallelize(Seq(Edge(3L, 7L, "collab"), Edge(5L, 3L, "advisor"),
        Edge(2L, 5L, "colleague"), Edge(5L, 7L, "pi")))

    val defaultUser = ("John Doe", "Missing")

    val graph: Graph[(String, String), String] = Graph(users, relationships, defaultUser)


    println(s"the graph vertices : ${graph.vertices.collect().mkString(", ")}")
    println(s"the graph edges : ${graph.edges.collect().mkString(", ")}")

    println(s"the graph every vertices inDegrees is ${graph.inDegrees.collect().mkString(", ")}")
    println(s"the graph every vertices outDegrees is ${graph.outDegrees.collect().mkString(", ")}")
    println(s"the graph every vertices degrees is ${graph.degrees.collect().mkString(", ")}")

    println(s"the graph vertices nums: ${graph.numVertices}")
    println(s"the graph edges nums: ${graph.numEdges}")

  }
}
